<template>
  <div class='container'>
    <el-dialog
      :title="title"
      :visible="dialogVisible"
      width="20%"
      @close="handleClose"
    >
      <el-form
        ref="formRef"
        :model="ruleForm"
        :rules="rules"
        label-width="80px"
      >
        <el-form-item
          label="学科名称"
          prop="subjectName"
        >
          <el-input
            v-model="ruleForm.subjectName"
            placeholder="请输入学科名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="是否显示">
          <el-switch
            v-model="ruleForm.isFrontDisplay"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-value="1"
            inactive-value="0"
          />

        </el-form-item>
      </el-form>
      <span
        slot="footer"
        class="dialog-footer"
      >
        <el-button @click="handleClose">取 消</el-button>
        <el-button
          type="primary"
          @click="handleClose(1)"
        >确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { update, add } from '@/api/hmmm/subjects'
export default {
  name: 'Dialog',
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      ruleForm: {
        subjectName: '',
        isFrontDisplay: '1'
      },
      rules: {
        subjectName: [
          { required: true, message: '请输入学科名称', trigger: ['change', 'blur'] }
        ]
      }
    }
  },
  computed: {
    title () {
      return this.ruleForm.id ? '修改学科' : '新增学科'
    }
  },
  methods: {
    async handleClose (val) {
      if (val === 1) {
        // console.log(this.ruleForm)
        if (this.ruleForm.id) {
          // const res = await update({ ...this.ruleForm, isFrontDisplay: +this.ruleForm.isFrontDisplay })
          const res = await update(this.ruleForm)
          // console.log(res)
          if (res.status !== 200) return this.$message.error('修改失败')
          this.$message.success('修改成功')
          // this.$emit('update')
        } else {
          const res = await add(this.ruleForm)
          console.log(res)
          if (res.status !== 200) return this.$message.error('新增失败')
          this.$message.success('新增成功')
        }
        this.$emit('update')
      }
      this.$refs.formRef.resetFields()
      this.ruleForm = { subjectName: '', isFrontDisplay: '1' }
      this.$emit('update:dialogVisible', false)
    }
  }
}
</script>

<style scoped lang='scss'></style>
